$(document).ready( function(){
	$("#search_input").on("focus", function(){
		$("#searchDropdown").removeClass("hidden");
	});
	
	$("#searchDropdown").on("click","a", function(){
		$("#searchDropdown").addClass("hidden");
		return this.href;
	});
	
	$("#searchDropdown").on("click", function(){
	});
	
	$("#search_input").on("keyup", function(){
		if($(this).val().length >= 3)
		{
			$("#searchDropdown .loader").removeClass("hidden");
			var dataString = "&search="+$(this).val();
			$.ajax({
			type:'POST',
			url: scriptsURL+"libs/ajax/ajax-includes.php",
			data: dataString,
			}).done(function(responseJSON){
				response = JSON.parse(responseJSON);
				if(response && response != "")
				{
					if(response.results)
					{
						$("#searchDropdown .searchResults").html(response.results);
					}
					else
					{
						$("#searchDropdown .searchResults").html("");
					}
					
					if(response.result_text)
					{
						$("#searchDropdown .resultText").html(response.result_text);
					}
					else
					{
						$("#searchDropdown .resultText").html("");
					}
				}
				$("#searchDropdown .loader").addClass("hidden");
			});
		}
	});
	
	$("body").on("click",function(event){
		if (!$(event.target).closest("#searchDropdown, #search_input").length) {
        	$("#searchDropdown").addClass("hidden");
    	}
	});
});